﻿using Domain.Event;
using Domain.IRepositorys;
using Domain.Services;
using Infrastructure.DomainEvent;
using Infrastructure.UniOfWorkBase;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Application.Event
{
    /// <summary>
    /// 任务审核通过 完成任务
    /// </summary>
    public class FinishTakeTaskEventHandler : IDomainEventHandler<FinishTakeTaskEvent>
    {
        public FeeService feeService { get; set; }
        public MsgPulishService msgPulishService { get; set; }
        public IEFUnitOfWork efUnitOfWork { get; set; }
        public IUserRepository userRepository { get; set; }
        public ITaskCategoryRepository taskCategoryRepository { get; set; }
        public ITaskRepository taskRepository { get; set; }
        public ITaskLogRepository taskLogRepository { get; set; }
        public IWalletRepository walletRepository { get; set; }
        public ITaskTakeRepository taskTakeRepository { get; set; }
        public override Task HandleAysnc(FinishTakeTaskEvent domainEvent, CancellationToken cancellationToken = default)
        {
            var takeTask = taskTakeRepository.GetByKey(domainEvent.TaskTakeID);
            var task = taskRepository.GetByKey(takeTask.TaskID);

            //处理用户收益和上线收益
            var v1 = feeService.HandleTaskUserFee(domainEvent.AuditUserID, task, takeTask);

            //处理新手奖励
            var v2=feeService.HandleNewUserReward(domainEvent.AuditUserID, task, takeTask);

            //上线拿十二元奖励处理
            var v3 = feeService.HandlePromoteTwekveReward(domainEvent.AuditUserID, task, takeTask);

            efUnitOfWork.Commit();

            foreach (var item in v1)
            {
                msgPulishService.SendMsg(item.Key, "任务佣金", item.Value);
            }
            foreach (var item in v2)
            {
                msgPulishService.SendMsg(item.Key, "新手任务", item.Value);
            }
            foreach (var item in v3)
            {
                msgPulishService.SendMsg(item.Key, "下线首次完成新手", item.Value);
            }
            return Task.CompletedTask;
        }
    }
    
}
